# !/usr/bin/env python
# -*-coding:utf-8 -*-
"""
# File       : department_view.py
# Time       ：2023/7/9 16:35
# Author     ：Y-aong
# version    ：python 3.7
# Description：
    部门视图
    dept views
"""
from flask_restx import fields

from apis.system_oauth import system_oauth_ns
from apis.system_oauth.models import SystemDepartment
from apis.system_oauth.schema.department_schema import SystemDepartmentSchema
from public.api_utils.api_decorator import handle_api_error
from public.base_response import MarshalModel
from public.base_view import BaseView, BaseDetailsView

department = {
    'department_name': fields.String(description='部门名称'),
    'pid': fields.Integer(description='上级部门ID'),
    'desc': fields.String(description='部门描述'),
}

department_marshal = MarshalModel('department', system_oauth_ns)


class DepartmentDetailView(BaseDetailsView):
    url = '/department/detail'

    def __init__(self, *args, **kwargs):
        super(DepartmentDetailView, self).__init__(*args, **kwargs)
        self.table_orm = SystemDepartment
        self.table_schema = SystemDepartmentSchema

    @handle_api_error()
    @system_oauth_ns.doc(description="获取部门详情")
    @system_oauth_ns.expect(department_marshal.detail_request())
    @system_oauth_ns.marshal_with(department_marshal.detail_response(department))
    def get(self):
        """获取部门详情"""
        return super(DepartmentDetailView, self).get()


class DepartmentView(BaseView):
    url = '/department'

    def __init__(self, *args, **kwargs):
        super(DepartmentView, self).__init__(*args, **kwargs)
        self.table_orm = SystemDepartment
        self.table_schema = SystemDepartmentSchema

    @handle_api_error()
    @system_oauth_ns.doc(description='获取部门列表')
    @system_oauth_ns.expect(department_marshal.list_request(department))
    @system_oauth_ns.marshal_with(department_marshal.list_response(department))
    def get(self):
        """获取部门列表"""
        return super(DepartmentView, self).get()

    @handle_api_error()
    @system_oauth_ns.doc(description='创建部门管理')
    @system_oauth_ns.expect(department_marshal.create_request(department))
    @system_oauth_ns.marshal_with(department_marshal.create_response())
    def post(self):
        """创建部门管理"""
        return super(DepartmentView, self).post()

    @handle_api_error()
    @system_oauth_ns.doc(description='修改部门管理')
    @system_oauth_ns.expect(department_marshal.update_request(department))
    @system_oauth_ns.marshal_with(department_marshal.update_response())
    def put(self):
        """修改部门管理"""
        return super(DepartmentView, self).put()

    @handle_api_error()
    @system_oauth_ns.doc(description='删除部门管理')
    @system_oauth_ns.expect(department_marshal.delete_request())
    @system_oauth_ns.marshal_with(department_marshal.delete_response())
    def delete(self):
        """删除部门管理"""
        return super(DepartmentView, self).delete()
